from datetime import date
from typing import Any, Generator

from models.person import Gender, IdentityCredentialsType, Person
from sqlalchemy import Engine
from sqlmodel import Session, SQLModel, create_engine

# 数据库链接；
sqlite_file_name = "sis.db"
DATABASE_URL: str = f"sqlite:///{sqlite_file_name}"

engine: Engine = create_engine(url=DATABASE_URL, echo=True, pool_pre_ping=True)


def get_session() -> Generator[Session, Any, None]:
    with Session(engine) as session:
        yield session


def init_db() -> None:
    """初始化数据库"""

    SQLModel.metadata.create_all(engine)


def init_data() -> None:
    """
    初始化数据。
    """

    with Session(engine) as session:
        persons: list[Person] = [
            Person(
                id=None,
                first_name="药师",
                last_name="黄",
                gender=Gender.MALE,
                id_type=IdentityCredentialsType.ID_CARD,
                id_number="500101194007310071",
                birth_date=date(1940, 7, 31),
            ),
            Person(
                id=None,
                first_name="智兴",
                last_name="段",
                gender=Gender.MALE,
                id_type=IdentityCredentialsType.ID_CARD,
                id_number="500101193201290099",
                birth_date=date(1932, 1, 29),
            ),
            Person(
                id=None,
                first_name="锋",
                last_name="欧阳",
                gender=Gender.MALE,
                id_type=IdentityCredentialsType.ID_CARD,
                id_number="500101193807070111",
                birth_date=date(1938, 7, 7),
            ),
            Person(
                id=None,
                first_name="七公",
                last_name="洪",
                gender=Gender.MALE,
                id_type=IdentityCredentialsType.ID_CARD,
                id_number="500101193004110056",
                birth_date=date(1930, 4, 11),
            ),
            Person(
                id=None,
                first_name="重阳",
                last_name="王",
                gender=Gender.MALE,
                id_type=IdentityCredentialsType.ID_CARD,
                id_number="500101193606170019",
                birth_date=date(1936, 6, 17),
            ),
            Person(
                id=None,
                first_name="靖",
                last_name="郭",
                gender=Gender.MALE,
                id_type=IdentityCredentialsType.ID_CARD,
                id_number="500101196412190157",
                birth_date=date(1964, 12, 19),
            ),
            Person(
                id=None,
                first_name="蓉",
                last_name="黄",
                gender=Gender.FEMALE,
                id_type=IdentityCredentialsType.ID_CARD,
                id_number="50010119660505026X",
                birth_date=date(1966, 5, 5),
            ),
            Person(
                id=None,
                first_name="康",
                last_name="杨",
                gender=Gender.MALE,
                id_type=IdentityCredentialsType.ID_CARD,
                id_number="500101196410070074",
                birth_date=date(1964, 10, 7),
            ),
            Person(
                id=None,
                first_name="念慈",
                last_name="穆",
                gender=Gender.FEMALE,
                id_type=IdentityCredentialsType.ID_CARD,
                id_number="500101196508300125",
                birth_date=date(1965, 8, 30),
            ),
            Person(
                id=None,
                first_name="芙",
                last_name="郭",
                gender=Gender.FEMALE,
                id_type=IdentityCredentialsType.ID_CARD,
                id_number="500101198903190027",
                birth_date=date(1989, 3, 19),
            ),
            Person(
                id=None,
                first_name="破虏",
                last_name="郭",
                gender=Gender.MALE,
                id_type=IdentityCredentialsType.ID_CARD,
                id_number="500103200311010018",
                birth_date=date(2003, 11, 15),
            ),
            Person(
                id=None,
                first_name="襄",
                last_name="郭",
                gender=Gender.FEMALE,
                id_type=IdentityCredentialsType.ID_CARD,
                id_number="500103200311010026",
                birth_date=date(2003, 11, 15),
            ),
            Person(
                id=None,
                first_name="过",
                last_name="杨",
                gender=Gender.MALE,
                id_type=IdentityCredentialsType.ID_CARD,
                id_number="50020819880825001X",
                birth_date=date(1988, 8, 25),
            ),
            Person(
                id=None,
                first_name="三丰",
                last_name="张",
                gender=Gender.MALE,
                id_type=IdentityCredentialsType.ID_CARD,
                id_number="500102200506050037",
                birth_date=date(2005, 6, 5),
            ),
        ]

        session.add_all(persons)
        session.commit()
